package co.baiku.boot.common.message;

import java.util.ArrayList;
import java.util.List;

public class PageData<T> {
    /**
     * 页码
     */
    private Integer pageNo;
    /**
     * 每页条数
     */
    private Integer pageSize;
    /**
     * 总页数
     */
    private Integer totalPage;
    /**
     * 结果总条数
     */
    private Integer totalCount;
    /**
     * 返回数据
     */
    private List<T> data;

    public static <T> PageData empty() {
        return empty(1, 10);
    }

    public static <T> PageData empty(int pageNo, int pageSize) {
        return new PageData<T>(pageNo, pageSize, 0, new ArrayList<T>());
    }

    public static <T> PageData of(Integer pageNo, Integer pageSize, Integer totalCount, List<T> data) {
        return new PageData<>(pageNo, pageSize, totalCount, data);
    }

    public PageData(Integer pageNo, Integer pageSize, Integer totalCount, List<T> data) {
        if (pageNo == null || pageNo <= 0) {
            pageNo = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 10;
        }
        if (totalCount == null || totalCount <= 0) {
            totalCount = 0;
        }
        this.pageNo = pageNo;
        this.pageSize = pageSize;
        this.totalCount = totalCount;
        this.data = data;
        if (totalCount > 0) {
            this.totalPage = (int) (totalCount / pageSize + (totalCount % pageSize == 0 ? 0 : 1));
        } else {
            this.totalPage = 0;
        }
    }

    public Integer getPageNo() {
        return pageNo;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

    public Integer getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(Integer totalCount) {
        this.totalCount = totalCount;
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }
}
